package com.view;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.controller.UserManager;
import com.domain.AppUser;

@SuppressWarnings("serial")
public class LogInServlet extends HttpServlet{
	private UserManager userManager = new UserManager();
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String password = req.getParameter("password");
		String username = req.getParameter("username");
		AppUser tempUser = new AppUser(username,password,null,null);
		boolean isValid = userManager.isUserValid(tempUser);
		//AppUser actualUser = userManager.searchAppUser(username);
		HttpSession actualSession = req.getSession(true);
		if(isValid){
			actualSession.setAttribute("UserName", username);
			actualSession.setAttribute("LoggedIn", true);
			//actualSession.setAttribute("AppUser", actualUser);
		}else{
			actualSession.setAttribute("LoggedIn",false);
		}
		/*String[] refererPath = req.getHeader("referer").split("/");
		int refererPathLength = refererPath.length;
		String redirectPath = refererPathLength > 0 ? refererPath[refererPathLength-1] : "/index.jsp";
		resp.sendRedirect(redirectPath);*/
		resp.sendRedirect("/index.jsp");
	}
}
